using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class CTBangKeNhapHangDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveCTBangKeNhapHang(CTBangKeNhapHang ctbangkenhaphang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddCTBangKeNhapHang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddCTBangKeNhapHang";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctbangkenhaphang.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdBangKe", DbType.Int32, ParameterDirection.Input, ctbangkenhaphang.IdBangKe);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenVatLieu", DbType.String, ParameterDirection.Input, ctbangkenhaphang.TenVatLieu);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaVatLieu", DbType.String, ParameterDirection.Input, ctbangkenhaphang.MaVatLieu);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonViTinh", DbType.String, ParameterDirection.Input, ctbangkenhaphang.DonViTinh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@KhoiLuong", DbType.Int64, ParameterDirection.Input, ctbangkenhaphang.KhoiLuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuong", DbType.Int64, ParameterDirection.Input, ctbangkenhaphang.SoLuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonGia", DbType.Int64, ParameterDirection.Input, ctbangkenhaphang.DonGia);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, ctbangkenhaphang.IsDelete);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
            conObj.Open();
            cmd.ExecuteNonQuery();
            conObj.Close();

            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			return result;
		}
		
		// Get all CTBangKeNhapHang in DB
		public ResultObj GetAllCTBangKeNhapHang( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTBangKeNhapHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTBangKeNhapHang", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTBangKeNhapHang>();
			CTBangKeNhapHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTBangKeNhapHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdBangKe = Convert.ToInt32(reader["IdBangKe"]);
			item.TenVatLieu = Convert.ToString(reader["TenVatLieu"]);
			item.MaVatLieu = Convert.ToString(reader["MaVatLieu"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.KhoiLuong = Convert.ToInt64(reader["KhoiLuong"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all CTBangKeNhapHang in DB with condition
		public ResultObj GetAllCTBangKeNhapHangByID(CTBangKeNhapHang ctbangkenhaphang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTBangKeNhapHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTBangKeNhapHangByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctbangkenhaphang.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTBangKeNhapHang>();
			CTBangKeNhapHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTBangKeNhapHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdBangKe = Convert.ToInt32(reader["IdBangKe"]);
			item.TenVatLieu = Convert.ToString(reader["TenVatLieu"]);
			item.MaVatLieu = Convert.ToString(reader["MaVatLieu"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.KhoiLuong = Convert.ToInt64(reader["KhoiLuong"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        // Get all CTBangKeNhapHang in DB with condition
        public ResultObj GetAllCTBangKeNhapHangByIdBangKe(CTBangKeNhapHang ctbangkenhaphang)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<CTBangKeNhapHang> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllCTBangKeNhapHangByIDBangKe", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@IdBangKe", DbType.Int32, ParameterDirection.Input, ctbangkenhaphang.IdBangKe);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<CTBangKeNhapHang>();
                CTBangKeNhapHang item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new CTBangKeNhapHang();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.IdBangKe = Convert.ToInt32(reader["IdBangKe"]);
                    item.TenVatLieu = Convert.ToString(reader["TenVatLieu"]);
                    item.MaVatLieu = Convert.ToString(reader["MaVatLieu"]);
                    item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
                    item.KhoiLuong = Convert.ToInt64(reader["KhoiLuong"]);
                    item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
                    item.DonGia = Convert.ToInt64(reader["DonGia"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

		// Delete a CTBangKeNhapHang out of DB with condition
		public ResultObj DeleteCTBangKeNhapHang(CTBangKeNhapHang ctbangkenhaphang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteCTBangKeNhapHang", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteCTBangKeNhapHang";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctbangkenhaphang.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of BangKeNhapHang from DB with condition
		public ResultObj GetBangKeNhapHang(int IdBangKeNhapHang)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<BangKeNhapHang> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetBangKeNhapHang", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdBangKeNhapHang);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<BangKeNhapHang>();
			BangKeNhapHang item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new BangKeNhapHang();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.NhaCungCap = Convert.ToString(reader["NhaCungCap"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
			item.KeToanTruong = Convert.ToString(reader["KeToanTruong"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
